package com.emuporium.gymrat.server.exercise;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.emuporium.gymrat.server.data.Equipment;
import com.emuporium.gymrat.server.data.Exercise;
import com.emuporium.gymrat.server.data.ExerciseDB;
import com.emuporium.gymrat.server.data.User;
import com.emuporium.gymrat.server.data.UserDB;
import com.emuporium.gymrat.server.data.Variation;

@SuppressWarnings("serial")
public class ExerciseInfoServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		int exerciseId = new Integer(request.getParameter("id"));
		User user = UserDB.selectUser(request.getRemoteUser());
		
		int userId; 
		if (user != null)
			userId = user.getId();
		else
			userId = 0;
		
		Exercise exercise = ExerciseDB.selectExercise(exerciseId, userId);
		ArrayList<Variation> allVariations = ExerciseDB.selectAllVariations(userId);
		ArrayList<Equipment> allEquipment = ExerciseDB.selectAllEquipment(userId);
		LinkedHashMap<Integer,String> allMuscleGroups = ExerciseDB.selectMuscleGroups();
		
		request.setAttribute("user", user);
		request.setAttribute("exercise", exercise);
		request.setAttribute("allVariations", allVariations);
		request.setAttribute("allEquipment", allEquipment);
		request.setAttribute("allMuscleGroups", allMuscleGroups);

		String url = "/exercise/info.jsp";
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher(url);
		dispatcher.forward(request, response);
	}

}
